package my.hnb.android.view.base;

import my.hnb.android.common.ActionEvent;
import my.hnb.android.common.ModelEvent;
import my.hnb.android.listenner.OnEventListener;
import my.hnb.android.ultils.AppUltils;
import my.hnb.songnhac.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Base fragment Xu ly cac phan chung: header, handle du lieu tu controller...
 * 
 * @author banghn
 */
public class BaseFragment extends Fragment implements OnEventListener {
	public static final String KN_ACTION = "my.hnb.android.songnhac";
	// activity chua view
	BaseActivity activity;
	// view root chua header & view fragment con
	LinearLayout viewRoot;
	// header trong fragment
	private View rlHeader;
	// header title
	private TextView tvTitle;
	// kiem tra fragment duoc finish, detroy chua
	public boolean isFinished = false;

	BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			int action = intent.getExtras().getInt("hnb.action.songnhac");
			int hasCode = intent.getExtras().getInt("hnb.hashCode.songnhac");
			if (hasCode != BaseFragment.this.hashCode()) {
				receiveBroadcast(action, intent.getExtras());
			}
		}
	};
	public boolean isZooming;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		try {
			IntentFilter filter = new IntentFilter(BaseActivity.BROADCAST_ACTION);
			activity.registerReceiver(receiver, filter);
		} catch (Exception e) {
		}
	}

	@Override
	public void onAttach(Activity activity) {
		this.activity = (BaseActivity) activity;
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		isFinished = false;
		super.onResume();
	}

	@Override
	public void onDestroyView() {
		try {
			getActivity().unregisterReceiver(receiver);
		} catch (Exception e) {
		}
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		isFinished = true;
		try {
			getActivity().unregisterReceiver(receiver);
		} catch (Exception e) {
		}
		super.onDestroy();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_base_main, null, false);
		viewRoot = (LinearLayout) view.findViewById(R.id.llContent);
		viewRoot.addView(container);
		isFinished = false;
		if (container != null) {
			container.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					AppUltils.forceHideKeyboard(activity);
					return false;
				}
			});
		}
		initHeaderview();
		return view;
	}

	/**
	 * Khoi tao add header view
	 * 
	 * @author : BangHN since : 4:30:53 PM
	 */
	private void initHeaderview() {
		// LayoutInflater inflater = (LayoutInflater) activity
		// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// rlHeader = inflater.inflate(R.layout.layout_fragment_header, null);
		// viewRoot.addView(rlHeader, 0);
		// tvTitle = (TextView)rlHeader.findViewById(R.id.tvTitle);
	}

	/**
	 * set title header view (fragment)
	 * 
	 * @author : BangHN since : 4:33:48 PM
	 */
	protected void setTitleHeaderView(String title) {
		if (tvTitle != null)
			tvTitle.setText(title);
	}

	protected void setTitleHeaderView(SpannableString title) {
		if (tvTitle != null)
			tvTitle.setText(title);
	}

	/**
	 * Send broadcast toi cac fragment khac
	 * 
	 * @author : BangHN since : 4:29:24 PM
	 */
	public void sendBroadcast(int action, Bundle bundle) {
		Intent intent = new Intent(BaseActivity.BROADCAST_ACTION);
		bundle.putInt("hnb.action.songnhac", action);
		bundle.putInt("hnb.hashCode.songnhac", intent.getClass().hashCode());
		intent.putExtras(bundle);
		activity.sendBroadcast(intent);
	}

	/**
	 * Nhan broadcast tu cac view fragment khac
	 * 
	 * @author : BangHN since : 4:29:39 PM
	 */
	protected void receiveBroadcast(int action, Bundle extras) {
		// TODO Auto-generated method stub

	}

	/**
	 * Nhan handle modle tu controller
	 * 
	 * @author : BangHN since : 4:29:55 PM
	 */
	public void handleModelViewEvent(ModelEvent modelEvent) {
		ActionEvent actionEvent = modelEvent.getActionEvent();
		switch (actionEvent.action) {
		default:
			break;
		}
	}

	/**
	 * Xu ly luong loi khi nhan tu controller
	 * 
	 * @author : BangHN since : 4:30:11 PM
	 */
	public void handleErrorModelViewEvent(ModelEvent modelEvent) {
		// TODO Auto-generated method stub
		ActionEvent actionEvent = modelEvent.getActionEvent();
		switch (actionEvent.action) {
		default:
			activity.handleErrorModelViewEvent(modelEvent);
			break;
		}

	}

	/**
	 * 
	 * Remove fragment ra khoi stack
	 * 
	 * @author: PhucNT
	 * @param promotionCode
	 * @return: void
	 * @throws:
	 */
	public void removeFragmentFromBackStack() {
		// go to promotion detail
		FragmentManager fm = this.getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		fm.popBackStack(this.getClass().getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
		ft.commit();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * An header fragment
	 * 
	 * @author: Tuanlt11
	 * @return: void
	 * @throws:
	 */
	public void hideHeaderView() {
		rlHeader.setVisibility(View.GONE);
	}

	public void closePopup() {

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onEvent(View v, int action, Object userData) {
		// TODO Auto-generated method stub

	}
}
